package com.crrc.dataparse.parse.PostAlg;

import com.crrc.dataparse.base.log.LogRecord;
import com.crrc.dataparse.entity.*;
import com.crrc.dataparse.parse.IPostAlgo;
import com.crrc.dataparse.util.ByteConvertUtil;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;

/*
    协议处理方案
    牵引单频率参数包使用
 */
public class SFPost implements IPostAlgo {
    private static final int NULL_PARAMETER = 4;
    private static final int BASE_INFO_PARAMETER_Line = 7;
    private static final int BASE_INFO_PARAMETER_Car = 8;
    private static final int BASE_INFO_PARAMETER_Terminal = 9;
    private static final int BASE_INFO_PARAMATER_TrainType = 11;

    public SFPost() {
    }

    @Override
    public int parseProtocolHead(String packetData, Protocol protocol, ParsedResult pdp, PaserInfo paserInfo) throws Exception {
        int offset = 0;
        byte[] data = ByteConvertUtil.binaryStringToBytes(packetData);

        TemplatePara para;
        for(Iterator var7 = protocol.getParaList().iterator(); var7.hasNext(); offset += para.getLength()) {
            Pro2TemPara p = (Pro2TemPara)var7.next();
            para = (TemplatePara)paserInfo.getIdParaMap().get(p.getParaOid());
            Integer paraType = para.getParameterType();

            //如果是预留参数，直接跳过
            if (paraType == NULL_PARAMETER) {
                continue;
            }
            if (paraType == BASE_INFO_PARAMETER_Line) {
                pdp.setlineNo(para.getParameterID());
            } else if (paraType == BASE_INFO_PARAMETER_Car) {
                pdp.setmetroNo(para.getParameterID());
            } else if (paraType == BASE_INFO_PARAMETER_Terminal) {
                pdp.setTerminal(para.getParameterID());
            } else if (paraType == BASE_INFO_PARAMATER_TrainType) {
                pdp.setTrainType(para.getParameterID());
            }


            pdp.addBaseInfo(para.getParameterID(), packetData.substring(offset, offset + para.getLength()));
            LogRecord.debug(new String[]{"基础信息key:", para.getParameterID(), "；基础信息Value:", packetData.substring(offset, offset + para.getLength())});
        }

        return offset;
    }
    @Override
    public Template getTemplate(String packetData, PaserInfo paserInfo, Protocol protocol) throws Exception {
        return protocol.getPro2TempList().get(0).getTemplate();
    }


    public ParsedResult postProcess_sf(ParsedResult pdp) throws Exception {
        pdp.setlineNo(pdp.getBaseInfoMap().get(pdp.getlineNo()));
        pdp.setmetroNo(pdp.getBaseInfoMap().get(pdp.getmetroNo()));
        pdp.setTerminal(pdp.getBaseInfoMap().get(pdp.getTerminal()));
        pdp.setTrainType(pdp.getBaseInfoMap().get(pdp.getTrainType()));
        long timestamp = Long.valueOf(pdp.getBaseInfoMap().get("Time"));
        pdp.setTimestamp(timestamp);
        return pdp;
    }
    public ParsedResult postProcess_mp(ParsedResult pdp,int increateTime) throws Exception {
        long timestamp = Long.valueOf(pdp.getBaseInfoMap().get("Time"));
        pdp.setTimestamp(timestamp+increateTime);
        return pdp;
    }
}
